﻿
using System;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;

namespace CodeCounsel.SharePoint.Flash
{
    [ToolboxData("<{0}:FlashVideo runat=server></{0}:FlashVideo>")]
    public class FlashVideo
        : Control
    {
        const string swfobjectResourceName = "CodeCounsel.SharePoint.Flash.swfobject.js";

        [DefaultValue(640),
        WebBrowsable,
        WebDescription("The width of the video area"),
        WebDisplayName("Video Width"),
        Personalizable(PersonalizationScope.User),
        Category("Flash")]
        public int VideoWidth
        {
            get { return (int?)ViewState["VideoWidth"] ?? 640; }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
                ViewState["VideoWidth"] = value;
            }
        }

        [DefaultValue(480),
        WebBrowsable,
        WebDescription("The height of the video area"),
        WebDisplayName("Video Height"),
        Personalizable(PersonalizationScope.User),
        Category("Flash")]
        public int VideoHeight
        {
            get { return (int?)ViewState["VideoHeight"] ?? 480; }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
                ViewState["VideoHeight"] = value;
            }
        }

        [DefaultValue(""),
        WebBrowsable,
        WebDescription("The video to display"),
        WebDisplayName("Video Source"),
        Personalizable(PersonalizationScope.Shared),
        Category("Flash")]
        public string VideoSource
        {
            get { return (string)ViewState["VideoSource"] ?? ""; }
            set { ViewState["VideoSource"] = value; }
        }

        [DefaultValue(false),
        WebBrowsable,
        WebDescription("Auto-start the video on page load."),
        WebDisplayName("Auto Start"),
        Personalizable(PersonalizationScope.Shared),
        Category("Flash")]
        public bool AutoStart
        {
            get { return (bool?)ViewState["AutoStart"] ?? false; }
            set { ViewState["AutoStart"] = value; }
        }

        [DefaultValue(""),
        WebBrowsable,
        WebDescription("The image to display when the video is loading."),
        WebDisplayName("Preview Image Location"),
        Personalizable(PersonalizationScope.Shared),
        Category("Flash")]
        public string PreviewImageUrl
        {
            get { return (string)ViewState["PreviewImageUrl"] ?? ""; }
            set { ViewState["PreviewImageUrl"] = value; }
        }

        [DefaultValue(true),
        WebBrowsable,
        WebDescription("Show the play icon in the middle of the display."),
        WebDisplayName("Show Icons"),
        Personalizable(PersonalizationScope.Shared),
        Category("Flash")]
        public bool ShowIcons
        {
            get { return (bool?)ViewState["ShowIcons"] ?? true; }
            set { ViewState["ShowIcons"] = value; }
        }

        [DefaultValue(true),
        WebBrowsable,
        WebDescription("How to display videos in full-screen mode"),
        WebDisplayName("Fullscreen Mode"),
        Personalizable(PersonalizationScope.Shared),
        Category("Flash")]
        public FullScreenMode FullScreenMode
        {
            get { return (FullScreenMode?)ViewState["FullScreenMode"] ?? FullScreenMode.NoStretch; }
            set { ViewState["FullScreenMode"] = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            EnsureID();
            int height = VideoHeight + 20;
            if (Page.ClientScript.IsClientScriptIncludeRegistered(
                typeof(FlashVideo), swfobjectResourceName) == false)
            {
                Page.ClientScript.RegisterClientScriptResource(
                    typeof(FlashVideo), swfobjectResourceName);
            }
            if (Page.ClientScript.IsStartupScriptRegistered(
                typeof(FlashVideo), "startup") == false)
            {
                string script = String.Format(
                  @"var videoObject = new SWFObject('{0}', 'mpl', '{1}', '{2}', '8');
                    videoObject.addParam('allowscriptaccess','always');
                    videoObject.addParam('allowfullscreen','true');
                    videoObject.addVariable('width','{1}');
                    videoObject.addVariable('height','{2}');
                    videoObject.addVariable('overstretch','{8}');
                    videoObject.addVariable('autostart','{5}');
                    videoObject.addVariable('file','{3}');
                    videoObject.addVariable('image','{6}');
                    videoObject.addVariable('showicons','{7}');
                    videoObject.write('{4}')",
                        "/_layouts/CCFlash/mediaplayer.swf",
                        VideoWidth,
                        height,
                        VideoSource,
                        ClientID,
                        AutoStart.ToString().ToLower(),
                        PreviewImageUrl,
                        ShowIcons.ToString().ToLower(),
                        FullScreenModeToString(FullScreenMode));

                Page.ClientScript.RegisterStartupScript(
                    typeof(FlashVideo), "startup",
                    script, true);
            }
            base.OnPreRender(e);
        }

        string FullScreenModeToString(FullScreenMode mode)
        {
            switch (mode)
            {
                case FullScreenMode.NoStretch:
                    return "false";
                case FullScreenMode.Proportional:
                    return "true";
                case FullScreenMode.Disproportional:
                    return "fit";
                case FullScreenMode.OriginalDimensions:
                    return "none";
                default:
                    throw new ApplicationException();
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            output.RenderBeginTag(HtmlTextWriterTag.Div);
            output.RenderEndTag();            
        }
    }
}